<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工姓名" prop="staffName">
        <el-input
          v-model="queryParams.staffName"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属部门" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:staff:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="staffList">
      <el-table-column :formatter="defaultTableColumnDate" label="ID" align="center" prop="id" />
      <el-table-column :formatter="defaultTableColumnDate" label="员工姓名" align="center" prop="staffName" />
      <el-table-column :formatter="defaultTableColumnDate" label="手机号码" align="center" prop="tel" />
      <el-table-column :formatter="defaultTableColumnDate" label="所属部门" align="center" prop="deptName" />
      <el-table-column :formatter="defaultTableColumnDate" label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.staff_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column :formatter="defaultTableColumnDate" label="续签预警" align="center" prop="status">
        <template slot-scope="scope">
          <span :style="{color: scope.row.needRemind? 'red': ''}">{{scope.row.contractExpirationTime}}</span>
        </template>
      </el-table-column>
      <el-table-column :formatter="defaultTableColumnDate" label="更新人" align="center" prop="updateBy" />
      <el-table-column :formatter="defaultTableColumnDate" label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :formatter="defaultTableColumnDate" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:staff:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:staff:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleForm(scope.row)"
            v-hasPermi="['system:staff:edit']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工管理对话框 -->
    <el-dialog destroy-on-close :title="title" :visible.sync="open" width="1000px" append-to-body>
      <p class="form-span-title">基础信息</p>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="照片" prop="url">
          <image-upload :limit="1" v-model="form.url"/>
        </el-form-item>
        <el-form-item label="员工姓名" prop="staffName">
          <el-input v-model="form.staffName" placeholder="请输入员工姓名" />
        </el-form-item>
        <el-form-item label="所属部门" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入所属部门" />
        </el-form-item>
        <el-form-item label="岗位" prop="post">
          <el-input v-model="form.post" placeholder="请输入岗位" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="手机号码" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="劳动合同到期时间" prop="contractExpirationTime">
          <el-date-picker clearable
            v-model="form.contractExpirationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择劳动合同到期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入职日期" prop="emplDate">
          <el-date-picker clearable
            v-model="form.emplDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入职日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="历史工龄(年)" prop="seniority">
          <el-input v-model="form.seniority" placeholder="请输入历史工龄(年)" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期" prop="birthDate">
          <el-date-picker clearable
            v-model="form.birthDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <p class="form-span-title">员工信息</p>
      <el-form label-width="150px">
        <el-form-item label="证件姓名">
          <el-input v-model="gwStaffBase.cardName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="民族">
          <el-input v-model="gwStaffBase.nation" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="户口类型">
          <el-input v-model="gwStaffBase.accountType" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="户口所在地">
          <el-input v-model="gwStaffBase.accountAddress" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="籍贯">
          <el-input v-model="gwStaffBase.nativePlace" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="最高学历">
          <el-input v-model="gwStaffBase.highestDegree" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="政治面貌">
          <el-input v-model="gwStaffBase.politics" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="婚姻状况">
          <el-input v-model="gwStaffBase.marital" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="微信号">
          <el-input v-model="gwStaffBase.wechat" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="个人邮箱">
          <el-input v-model="gwStaffBase.email" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="有无刑事犯罪记录">
          <el-input v-model="gwStaffBase.izPenal" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="与前单位有无劳动争议或竞业限制">
          <el-input v-model="gwStaffBase.izDisputed" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <p class="form-span-title">教育经历</p>
      <el-form ref="gwStaffEducations" label-width="150px">
        <div v-for="(item, index) in gwStaffEducations" :key="index">
          <el-form-item label="学校名称">
            <el-input v-model="item.schoolName" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="专业名称">
            <el-input v-model="item.major" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="入校日期">
            <el-date-picker clearable
              v-model="item.enterDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="毕业日期">
            <el-date-picker clearable
              v-model="item.graduationDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="学历">
            <el-input v-model="item.education" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="是否取得学位">
            <el-input v-model="item.izGet" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="证书照片" prop="url">
            <image-upload v-model="item.url"/>
          </el-form-item>
          <el-divider></el-divider>
        </div>
        <el-form-item>
          <el-button type="primary" @click="gwStaffEducations.push({})">添加教育经历</el-button>
          <el-button v-if="gwStaffEducations.length > 1" @click="gwStaffEducations.pop()">删除教育经历</el-button>
        </el-form-item>
      </el-form>
      <p class="form-span-title">工作经历</p>
      <el-form ref="gwStaffWorks" label-width="150px">
        <div v-for="(item, index) in gwStaffWorks" :key="index">
          <el-form-item label="公司名称">
            <el-input v-model="item.company" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="担任职位">
            <el-input v-model="item.post" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="入职日期">
            <el-date-picker clearable
              v-model="item.emplDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="离职日期">
            <el-date-picker clearable
              v-model="item.resignDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="职位描述">
            <el-input v-model="item.postDesc" type="textarea" placeholder="请输入" />
          </el-form-item>
          <el-divider></el-divider>
        </div>
        <el-form-item>
          <el-button type="primary" @click="gwStaffWorks.push({})">添加工作经历</el-button>
          <el-button v-if="gwStaffWorks.length > 1" @click="gwStaffWorks.pop()">删除工作经历</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="sendStaff" v-if="!form.id" >发送给员工填写</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查看 -->
    <el-dialog destroy-on-close :title="title" :visible.sync="formopen" width="1000px" append-to-body>
      <div class=""></div>
      <div style="padding: 50px;">
        <p class="form-span-title">基础信息</p>
        <div style="gap: 60px;display: flex;margin-bottom: 40px;">
          <image-preview :src="form.url" :width="200" />
          <el-switch
            style="flex-shrink: 0;"
            v-model="form.status"
            disabled
            active-text="在职"
            inactive-text="离职"
            active-value="在职"
            inactive-value="离职"
            @change="statusChange">
          </el-switch>
        </div>
        <div class="descriptions descriptions-1">
          <div class="descriptions-item">
            <div class="descriptions-lable">员工姓名</div>
            <div class="descriptions-content">{{ form.staffName }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">所属部门</div>
            <div class="descriptions-content">{{ form.deptName }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">岗位</div>
            <div class="descriptions-content">{{ form.post }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">身份证号</div>
            <div class="descriptions-content">{{ form.idCard }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">手机号码</div>
            <div class="descriptions-content">{{ form.tel }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">合同到期时间</div>
            <div class="descriptions-content">{{ form.contractExpirationTime }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">入职日期</div>
            <div class="descriptions-content">{{ form.emplDate }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">历史工龄(年)</div>
            <div class="descriptions-content">{{ form.seniority }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">性别</div>
            <div class="descriptions-content">{{ form.sex }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">出生日期</div>
            <div class="descriptions-content">{{ form.birthDate }}</div>
          </div>
        </div>
        <p class="form-span-title">员工信息</p>
        <div class="descriptions descriptions-1">
          <div class="descriptions-item">
            <div class="descriptions-lable">证件姓名</div>
            <div class="descriptions-content">{{ gwStaffBase.cardName }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">民族</div>
            <div class="descriptions-content">{{ gwStaffBase.nation }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">户口类型</div>
            <div class="descriptions-content">{{ gwStaffBase.accountType }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">户口所在地</div>
            <div class="descriptions-content">{{ gwStaffBase.accountAddress }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">籍贯</div>
            <div class="descriptions-content">{{ gwStaffBase.nativePlace }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">最高学历</div>
            <div class="descriptions-content">{{ gwStaffBase.highestDegree }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">政治面貌</div>
            <div class="descriptions-content">{{ gwStaffBase.politics }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">婚姻状况</div>
            <div class="descriptions-content">{{ gwStaffBase.marital }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">微信号</div>
            <div class="descriptions-content">{{ gwStaffBase.wechat }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">个人邮箱</div>
            <div class="descriptions-content">{{ gwStaffBase.email }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">有无刑事犯罪记录</div>
            <div class="descriptions-content">{{ gwStaffBase.izPenal }}</div>
          </div>
          <div class="descriptions-item">
            <div class="descriptions-lable">与前单位有无劳动争议或竞业协议</div>
            <div class="descriptions-content">{{ gwStaffBase.izDisputed }}</div>
          </div>
        </div>
        <p class="form-span-title">教育经历</p>
        <div class="a-card" v-for="(item, index) in gwStaffEducations" :key="index">
          <div class="a-card-time">{{ item.enterDate }}至{{ item.graduationDate }}</div>
          <div class="a-card-flex-content">
            <div v-if="item.schoolName">{{ item.schoolName }}</div>
            <div v-if="item.major">{{ item.major }}</div>
          </div>
          <div class="a-card-flex-content">
            <div v-if="item.education">{{ item.education }}</div>
            <div v-if="item.izGet">是否取得学历:{{ item.izGet }}</div>
          </div>
          <image-preview v-if="item.url" :src="item.url" :width="200" />
          <!-- <img v-if="item.url" class="a-card-img-content" :src="baseUrl + item.url"></img> -->
        </div>
        <p class="form-span-title">工作经历</p>
        <div class="a-card" v-for="(item, index) in gwStaffWorks" :key="index">
          <div class="a-card-time">{{ item.emplDate }}至{{ item.resignDate }}</div>
          <div class="a-card-flex-content">
            <div v-if="item.company">{{ item.company }}</div>
            <div v-if="item.post">{{ item.post }}</div>
          </div>
          <div v-if="item.postDesc" class="a-card-content">{{ item.postDesc }}</div>
        </div>
        <p class="form-span-title">签署的合同</p>
        <div class="gwContracts-item" v-for="item in form.gwContracts">
          <div>
            <div class="title">{{ item.contractName }}</div>
            <div class="time">{{ item.createTime }}</div>
          </div>
          <div class="status">{{ item.statusName }}</div>
        </div>
        <el-empty v-if="form.gwContracts && form.gwContracts.length == 0" description="暂无数据"></el-empty>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStaff, getStaff, delStaff, addStaff, updateStaff, addByStaff } from "@/api/system/staff";

export default {
  name: "Staff",
  dicts: ['staff_status', 'sys_user_sex'],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工管理表格数据
      staffList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      formopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        url: null,
        staffName: null,
        deptName: null,
        post: null,
        idCard: null,
        tel: null,
        contractExpirationTime: null,
        emplDate: null,
        seniority: null,
        sex: null,
        birthDate: null,
        status: null,
      },
      // 表单参数
      form: {},
      gwStaffBase: {},
      gwStaffEducations: [],
      gwStaffWorks: [],
      // 表单校验
      rules: {
        staffName: [
          { required: true, message: "员工姓名不能为空", trigger: "blur" }
        ],
        post: [
          { required: true, message: "岗位不能为空", trigger: "blur" }
        ],
        deptName: [
          { required: true, message: "所属部门不能为空", trigger: "blur" }
        ],
        idCard: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        tel: [
          { required: true, message: "手机号码不能为空", trigger: "blur" }
        ],
        contractExpirationTime: [
          { required: true, message: "劳动合同到期时间不能为空", trigger: "blur" }
        ],
        emplDate: [
          { required: true, message: "入职日期不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询员工管理列表 */
    getList() {
      this.loading = true;
      listStaff(this.queryParams).then(response => {
        this.staffList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.formopen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyId: null,
        url: null,
        staffName: null,
        deptName: null,
        post: null,
        idCard: null,
        tel: null,
        contractExpirationTime: null,
        emplDate: null,
        seniority: null,
        sex: null,
        birthDate: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.gwStaffBase = {}
      this.gwStaffEducations = []
      this.gwStaffWorks = []
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加员工管理";
      this.gwStaffEducations = [{}]
      this.gwStaffWorks = [{}]
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStaff(id).then(response => {
        this.form = response.data;
        this.gwStaffBase = response.data.gwStaffBase || {};
        this.gwStaffWorks = response.data.gwStaffWorks;
        this.gwStaffEducations = response.data.gwStaffEducations;
        this.open = true;
        this.title = "修改员工管理";
      });
    },
    /** 修改按钮操作 */
    handleForm(row) {
      this.reset();
      const id = row.id || this.ids
      getStaff(id).then(response => {
        this.form = response.data;
        this.gwStaffBase = response.data.gwStaffBase || {};
        this.gwStaffWorks = response.data.gwStaffWorks;
        this.gwStaffEducations = response.data.gwStaffEducations;
        this.formopen = true;
        this.title = "查看";
      });
    },
    sendStaff() {
      const that = this
      that.$refs["form"].validate(valid => {
        if (valid) {
          const params = {
            ...that.form,
          }
          addByStaff(params).then(response => {
            this.$modal.msgSuccess("发送成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      const that = this
      that.$refs["form"].validate(valid => {
        if (valid) {
          const params = {
            ...that.form,
            gwStaffBase: that.gwStaffBase,
            gwStaffWorks: that.gwStaffWorks,
            gwStaffEducations: that.gwStaffEducations,
          }
          if (this.form.id != null) {
            updateStaff(params).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStaff(params).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 修改状态
    statusChange(n) {
      updateStaff({id: this.form.id, status: n}).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('删除后不可恢复，是否确认删除？').then(function() {
        return delStaff(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/staff/export', {
        ...this.queryParams
      }, `staff_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
